<?php

/**
 * @version		1.5.x
 * @package		JoomlaVi Project
 * @copyright	Copyright (C) JoomlaVi. All rights reserved.
 * @license		Proprietary - JoomlaVi Club members only
 * @email       info@joomlavi.com 
 */
// No direct access to this file
defined('JPATH_BASE') or die;

/**
 * Renders a module list element
 *
 */

class JElementModule extends JElement
{
	/**
	* Element type
	*
	* @access	protected
	* @var		string
	*/
	var	$_name = 'Module';

	function fetchElement($name, $value, &$node, $control_name)
	{
		$options = array ();
        $db      = JFactory::getDBO();
        
        $query =  ' SELECT DISTINCT position' 
        		. ' FROM #__modules AS a' 
        		. ' WHERE a.published = 1'
        		. ' AND a.position <> \'cpanel\'' 
        		. ' ORDER BY a.position';
                
		$db->setQuery( $query );                
		$result = $db->loadObjectList();
        
        // Initialize variables.
		$html = array();
		$attr = '';

		// Initialize some field attributes.
		$attr .= $node->attributes('class') ? ' class="combobox '.(string) $node->attributes('class').'"' : ' class="combobox"';
		$attr .= ((string) $node->attributes('readonly') == 'true') ? ' readonly="readonly"' : '';
		$attr .= ((string) $node->attributes('disabled') == 'true') ? ' disabled="disabled"' : '';
		$attr .= $node->attributes('size') ? ' size="'.(int) $node->attributes('size').'"' : '';

		// Initialize JavaScript field attributes.
		$attr .= $node->attributes('onchange') ? ' onchange="'.(string) $node->attributes('onchange').'"' : '';
        
        
        // Load the combobox behavior.
		JHTML::_('behavior.combobox');
        
        // Build the input for the combo box.
		$html[] = '<input type="text" name="'.''.$control_name.'['.$name.']'.'" id="'.$control_name.$name.'"' .
				' value="'.htmlspecialchars($value, ENT_COMPAT, 'UTF-8').'"'.$attr.'/>';

		// Build the list for the combo box.
		$html[] = '<ul id="combobox-'.$control_name.$name.'" style="display:none;">';
        
		foreach ($result as $option) {
			$html[] = '<li>'.$option->position.'</li>';
		}
		$html[] = '</ul>';
        
        return implode($html);
        
	}
}
